object.js ➔ isEmpty   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
1
import { isObject, isArray } from './checkType';
2
3
/**
4
 * cleans object from undefined fields recursievly
5
 * @param {Object} obj primitive for examination
6
 * @returns {Object} object without undefined fields
7
 */
8
export function cleanUndefined(obj, { cache = new Set() } = {}) {
9
    cache.add(obj);
10
    for (const key of Object.keys(obj)) {
11
        if (obj[key] === undefined) delete obj[key]; // eslint-disable-line no-param-reassign
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
12
        if (isObject(obj[key])) {
13
            if (cache.has(obj[key])) continue;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
            cleanUndefined(obj[key], { cache });
15
        }
16
    }
17
18
    return obj;
19
}
20
21
/**
22
 * create deep clone of object
23
 * @param {Object} x input object
24
 * @returns {Object} deep copy
25
 *
26
 * Note that object can't have circular references
27
 */
28
export function clone(x) {
29
    return JSON.parse(JSON.stringify(x));
30
}
31
32
/**
33
 * determines whether the object or array is empty
34
 * @param {any} x primitive for examination
35
 * @returns {boolean} true if x is empty or false otherwise
36
 */
37
export function isEmpty(x) {
38
    const isObjectEmpty = isObject(x) && Object.keys(x).length === 0;
39
    const isArrayEmpty = isArray(x) && x.keys(x).length === 0;
40
41
    return isObjectEmpty || isArrayEmpty;
42
}
43